package net.midnighttd.game.view;

import net.midnighttd.engine.Engine;
import net.midnighttd.game.StateChangeListener;
import net.midnighttd.game.GameState;
import net.midnighttd.game.MidnightTD;
import net.midnighttd.engine.ResourceManager;
import net.midnighttd.engine.view.EngineView;
import net.midnighttd.game.resource.AppletResourceManager;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Author: Adam Scarr
 * Date: 22/04/2009
 * Time: 12:59:38 PM
 */
public class GameApplet extends JApplet implements MainUI {
    private Sidepanel side;
    private EngineView view;
    private MidnightTD game;
    private boolean inited = false;
    private Timer repaintTimer;
    private BufferedImage backBuffer;

    public void start() {
        ResourceManager.setInstance(new AppletResourceManager(this));
        repaint();
        game = new MidnightTD();

        new Thread(new Runnable() {
            public void run() {
                game.init("data/easy/game.xml");
                gameInitComplete();
            }
        }).start();
     
        backBuffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_3BYTE_BGR);

        Container container = super.getContentPane();

        container.setLayout(new BorderLayout());

        view = new MidnightTDView(this);
        container.add(view, BorderLayout.CENTER);

        side = new Sidepanel(this);
        container.add(new StatBar(this), BorderLayout.NORTH);
        container.add(side, BorderLayout.EAST);
        setVisible(true);

        repaintTimer = new Timer();
        repaintTimer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                repaint();
            }
        }, 0, 10);
    }

    public void gameInitComplete()  {
        System.out.println("Engine reinit complete, Starting engine...");

        repaintTimer.cancel();

        inited = true;

        side.reinit();
        view.reinit();

        game.start();
        repaint();
    }

    public TowerSelector getTowerSelector() {
        return side.getTowerSelector();
    }

    public MidnightTD getGame() {
        return game;
    }

    public Sidepanel getSidepanel() {
        return side;
    }


    public void paint(Graphics g) {
        if(inited) {
            super.paint(g);
        } else {
            Graphics f = backBuffer.getGraphics();
            f.setColor(new Color(37, 34, 30));
            f.fillRect(0, 0, getWidth(), getHeight());

            f.setColor(new Color(200, 190, 186));
            f.setFont(new Font("Arial", Font.PLAIN, 32));
            f.drawString("Loading...", 200, 250);

            f.setColor(new Color(160, 150, 140));
            f.setFont(new Font("Arial", Font.PLAIN, 12));
            f.drawString(ResourceManager.getLastLoaded(), 230, 280);

            g.drawImage(backBuffer, 0, 0, null);
        }
    }
}
